package com.moyu.article.controller;

import com.moyu.article.domain.post.Post;
import com.moyu.article.domain.post.dto.*;
import com.moyu.article.domain.post.vo.BoardOptionVO;
import com.moyu.article.domain.post.vo.PostQueryVO;
import com.moyu.article.service.IBoardService;
import com.moyu.article.service.IPostService;
import com.moyu.common.core.contants.HttpConstants;
import com.moyu.common.core.controller.BaseController;
import com.moyu.common.core.domain.R;
import com.moyu.common.core.domain.Result;
import com.moyu.common.core.domain.TableDataInfo;
import com.moyu.common.security.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author 赵彦博
 * @Date 2025/3/29 19:06
 */
@RestController
@RequestMapping("/post")
public class PostController  extends BaseController {

    @Autowired
    private IPostService postService;

    @Autowired
    private IBoardService boardService;

    @Autowired
    private TokenService tokenService;

    @GetMapping("/list")
    public TableDataInfo getPostList(PostQueryDTO postQueryDTO) {
        return getTableDataInfo(postService.getPostList(postQueryDTO));
    }

    @PutMapping("/approve")
    public R updatePost(@RequestBody PostUpdateStateDTO postUpdateStateDTO) {
        return toR(postService.updatePost(postUpdateStateDTO));
    }

    @GetMapping("/boardOptions")
    public R<List<BoardOptionVO>> getBoardOptions() {
        List<BoardOptionVO> options = boardService.getBoardOptions();
        return R.ok(options);
    }

    @GetMapping("/newest")
    public TableDataInfo getListByNewest(PostQueryListDTO dto) {
        return getTableDataInfo(postService.getListByNewest(dto));
    }

    @GetMapping("/hot")
    public TableDataInfo getListByHot(PostQueryListDTO dto) {
        return getTableDataInfo(postService.getListByHot(dto));
    }

    @GetMapping("/unanswered")
    public TableDataInfo getListByUnanswered(PostQueryListDTO dto) {
        return getTableDataInfo(postService.getListByUnanswered(dto));
    }

    @PostMapping("/createPost")
    public R createPost(@RequestBody PostSaveDTO saveDTO) {
        return toR(postService.createPost(saveDTO));
    }

    @GetMapping("/isLike")
    public Boolean getIsLike(PostIsLikeDTO dto) {
        return postService.getIsLike(dto);
    }

    @PostMapping("/like")
    public int insertLikeWithPost(PostIsLikeDTO dto) {
        return postService.insertLikeWithPost(dto);
    }

    @DeleteMapping("/deleteLike")
    public int deleteLikeWithPost(PostIsLikeDTO dto) {
        return postService.deleteLikeWithPost(dto);
    }


    /**
     * 获取帖子详情
     */
    @GetMapping("/{postId}")
    public PostQueryVO getPostDetail(@PathVariable Long postId) {
//        Long userId = tokenService.getLoginUserId();
        PostQueryVO post = postService.getPostDetail(postId);
        return post;
    }

    /**
     * 点赞帖子
     */
    @PostMapping("/{postId}/like")
    public Boolean likePost(@PathVariable Long postId) {
        Long userId = tokenService.getLoginUserId();
        if (userId == null) {
            return false;
        }
        postService.likePost(postId, userId);
        return true;
    }

    /**
     * 取消点赞帖子
     */
    @DeleteMapping("/{postId}/cancelLike")
    public Boolean cancelLikePost(@PathVariable Long postId) {
        Long userId = tokenService.getLoginUserId();
        if (userId == null) {
            return false;
        }
        postService.cancelLikePost(postId, userId);
        return true;
    }

    /**
     * 检查帖子点赞状态
     */
    @GetMapping("/{postId}/like-status")
    public Boolean checkLikeStatus(@PathVariable Long postId) {
        Long userId = tokenService.getLoginUserId();
        if (userId == null) {
            return false;
        }
        boolean isLiked = postService.checkLikeStatus(postId, userId);
        return isLiked;
    }

    @DeleteMapping("/deletePost/{postId}")
    public R deletePost(@PathVariable String postId) {
        return toR(postService.deletePost(postId));
    }

    @PutMapping("/updatePost")
    public R updatePostByUser(@RequestBody PostUpdateDTO dto) {
        return toR(postService.updatePostByUser(dto));
    }


    @GetMapping("/{postId}/favorite-status")
    public Boolean checkFavoriteStatus(@RequestHeader(HttpConstants.AUTHENTICATION) String token, @PathVariable String postId) {
        return postService.checkFavoriteStatus(token,postId);
    }

    @PostMapping("/{postId}/favorite")
    public R favoritePost(@RequestHeader(HttpConstants.AUTHENTICATION) String token, @PathVariable String postId) {
        return toR(postService.favoritePost(token,postId));
    }

    @DeleteMapping("/{postId}/cancelFavorite")
    public R cancelFavoritePost(@RequestHeader(HttpConstants.AUTHENTICATION) String token, @PathVariable String postId) {
        return toR(postService.cancelFavoritePost(token,postId));
    }
}
